<!DOCTYPE html>
<html lang="zh-cn"><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/main.css" rel="stylesheet">

    <link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico" />

    <title>docker学习笔记 | wiseAI的小站</title>
</head><body><div class="container-fluid">
    <nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">
                <img src="/img/favicon.ico" alt="" width="30" height="24" class="d-inline-block align-text-top">
                wiseAI的小站
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/articles/">文章</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/categories/">分类</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/tags/">关键字</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/post/golang/">golang编程</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/about/">关于</a>
                    </li>
                    
                </ul>
                <form class="d-flex">
                    <input id="search-query" class="form-control me-2" type="search" placeholder="Search for anything..." aria-label="Search">
                </form>
            </div>
        </div>
    </nav>
</div>




<div id="content">
<div class="container article">
	<h1>docker学习笔记</h1>
	<div class="post-meta">
		<div>
			
			
			By wiseai | 2022-05-30 | 2 minutes
		</div>
		<div class="tags">
			
			<a class="btn btn-light links btn-sm" href="/tags/docker/">docker</a>
			
			<a class="btn btn-light links btn-sm" href="/tags/%E5%AE%B9%E5%99%A8/">容器</a>
			
		</div>
	</div>
	<div>
		<div class="article-post">
			<p>现在docker分企业版和社区版，如果自己用的话，还是社区版吧！！！</p>
<h2 id="安装">安装：</h2>
<p>centos7：</p>
<p>1.卸载旧版本</p>
<pre tabindex="0"><code>$ yum remove docker \
             docker-client \
             docker-client-latest \
             docker-common \
             docker-latest \
             docker-latest-logrotate \
             docker-logrotate \
             docker-selinux \
             docker-engine-selinux \
             docker-engine
</code></pre><p>2.安装依赖包：</p>
<p><code>$ yum install -y yum-utils device-mapper-persistent-data lvm2</code></p>
<p>3.安装docker官方库：</p>
<p><code>$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo</code></p>
<p>4.安装最新稳定版本docker：</p>
<p><code>$ yum install docker-ce</code></p>
<p>如果提示接受GPG密钥，请验证指纹是否匹配<code>060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35</code>，如果匹配 ，则接受它。</p>
<p>ubuntu:</p>
<pre tabindex="0"><code>    $ sudo apt-get update
    $ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg &#39;sudo pt-key add -&#39;
    $ sudo add-apt-repository &#34;deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&#34;
    $ sudo apt-get update
</code></pre><p>其它发行版本的请参考：<a href="https://docs.docker.com/install/linux/docker-ce/ubuntu/#os-requirements">官方文档</a></p>
<p>配置：</p>
<p>1.Linux更改Docker运行根目录</p>
<p>Docker 的配置文件可以设置大部分的后台进程参数，在各个操作系统中的存放位置不一致:</p>
<p>在 ubuntu 中的位置是：/etc/default/docker</p>
<p>在 centos 中的位置是：/etc/sysconfig/docker</p>
<p>只需要更改 vim /etc/sysconfig/docker 成</p>
<p>为OPTIONS选项添加参数：&ndash;graph=/home/docker &ndash;icc=false &ndash;graph=/your/dir</p>
<p>&ndash;icc=false禁用容器内部通信，为安全建议添加，内部通信使用link</p>
<p>&ndash;graph=/your/dir 自定义运行目录</p>
<p>或者使用软件链接：</p>
<p><code># ln -sv /home/docker/ /var/lib/docker</code></p>
<p>搞定！</p>
<p>2.iptables设置：</p>
<p><code># iptables -A INPUT -s 172.17.0.0/16 -d 172.17.0.0/16 -j ACCEPT</code></p>
<p>命令：</p>
<ul>
<li>
<p>查找镜像：docker search centos</p>
</li>
<li>
<p>下载镜像：docker pull centos</p>
</li>
<li>
<p>列出本地镜像：docker images</p>
</li>
<li>
<p>删除镜像：docker rmi 镜像名</p>
</li>
<li>
<p>运行容器：
<code># docker run -it --name 容器名 --link 链接容器名:别名 -v 本地绝对路径:容器路径  -p ip:本地端口:容器端口/udp --restart=always （docker服务启动后容器自动启动，no 不启动；on-failure 容器退出状态非0时重启）-e MYSQL_ROOT_PASSWORD='passwd'(设置环境变量） --dns=自定义的DNS -d(后台运行) 镜像 运行程序(这里的程序可以是/bin/下的程序，可以将shell放到bin下)</code></p>
</li>
<li>
<p>列出运行容器：docker container ls</p>
</li>
<li>
<p>列出所有容器：docker container ls -a</p>
</li>
<li>
<p>运行容器：docker start 容器名（-i参数进入命令行）</p>
</li>
<li>
<p>进入运行容器：docker exec -it 容器名 /bin/bash</p>
</li>
<li>
<p>停止容器：docker stop 容器名</p>
</li>
<li>
<p>kill容器：docker kill 容器名</p>
</li>
<li>
<p>删除容器：docker rm 容器名</p>
</li>
<li>
<p>查看容器信息：docker inspect 容器名</p>
</li>
<li>
<p>查看容器log：docker logs 容器名</p>
</li>
<li>
<p>利用已有容器修改或建立新的镜像：docker commit -m &ldquo;说明信息&rdquo; -a &ldquo;用户信息&rdquo; 容器名 centos7/nginx-php:v2</p>
</li>
<li>
<p>镜像存出：docker save -o ubuntu_14.04.tar ubuntu:14.04或docker save &gt; ubuntu_14.04.tar ubuntu:14.04</p>
</li>
<li>
<p>镜像载入：docker load -i ubuntu_14.04.tar或docker load &lt; ubuntu_14.04.tar</p>
</li>
<li>
<p>容器导出：docker export ubuntu &gt; ubuntu.tar</p>
</li>
<li>
<p>容器导入：docker import ubuntu.tar test/ubuntu
注意：
容器导入后就成镜像了，如果容器有运行的命令，在run的时候也一定要加上命令，否则不能成功创建。
镜像就不存在这个问题</p>
</li>
<li>
<p>从主机复制到容器：docker cp host_path containerID:container_path</p>
</li>
<li>
<p>从容器复制到主机：docker cp containerID:container_path host_path</p>
</li>
<li>
<p>最后添加&quot;运行命令&quot;的shell里，如果没有驻留服务的话，一定要在最后一行写bash，否则无法启动。</p>
</li>
</ul>
<h2 id="docker网络设置">docker网络设置：</h2>
<p>docker安装后，默认会创建三种网络类型，bridge、host和none，可通过如下命令查看：</p>
<p><code># docker network ls</code></p>
<p><strong>bridge：网络桥接</strong></p>
<p>默认情况下启动、创建容器都是用该模式，所以每次docker容器重启时会按照顺序获取对应IP地址，这就导致容器每次重启，IP都发生变化，这种类型下无法设置固定IP</p>
<p><strong>none：无指定网络</strong></p>
<p>启动容器时，可以通过-network=none，docker容器不会分配局域网ip</p>
<p><strong>host：主机网络</strong></p>
<p>docker容器的网络会附属在主机上，两者是互通的。这种类型下可以设置固定IP</p>
<h3 id="创建固定ip容器">创建固定ip容器：</h3>
<h4 id="1创建自定义网络类型并且指定网段">1.创建自定义网络类型，并且指定网段</h4>
<p><code># docker network create --subnet=192.168.168.0/30 mynet</code>
通过docker network ls可以查看到网络类型中多了一个mynet</p>
<h4 id="2使用新的网络类型创建并启动容器">2.使用新的网络类型创建并启动容器</h4>
<p><code># docker run -it --name test --net mynet --ip 192.168.168.2 centos /bin/bash</code></p>
<h2 id="存在问题">存在问题：</h2>
<ul>
<li>docker-storage-setup不能启动的问题。
编辑/etc/sysconfig/docker-storage-setup文件
添加STORAGE_DRIVER=&ldquo;overlay&rdquo;</li>
<li>容器启动自动运行
编辑/etc/bashrc文件
添加运行命令在文件最后</li>
</ul>

		</div>
	</div>
</div>


<div class="container">
    
    <div class="row">
        
        <div class="col-5">
            <a class="page-link link-dark text-end dh" href="/post/efi%E5%92%8Cgrub2%E7%9B%B8%E5%85%B3%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%8F%8A%E5%91%BD%E4%BB%A4/">
                <h5>前一篇</h5><br>
                efi和grub2相关配置文件及命令
            </a>            
        </div>
        
        <div class="col-2">
        </div>
        
        <div class="col-5">
            <a class="page-link link-dark text-start dh" href="/post/adduseraddgroup%E6%B7%BB%E5%8A%A0%E7%94%A8%E6%88%B7%E7%94%A8%E6%88%B7%E7%BB%84/">
                <h5>后一篇</h5><br>
                adduser&amp;addgroup添加用户&amp;用户组
            </a>            
        </div>
        
    </div>
    
</div>

        </div><br><br>
<footer class="container">
    <h2>友情链接</h2>
    <hr>
    <nav class="nav nav-pills flex-column flex-sm-row">
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/gnzg/index.html" target="_blank">孤鸟之歌</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/mm/index.html" target="_blank">生成随机字符</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/md/index.html" target="_blank">MarkDown</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/" target="_blank">Gitee</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://github.com/" target="_blank">Github</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyun.com/" target="_blank">阿里云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cloud.tencent.com/" target="_blank">腾讯云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.oschina.net/" target="_blank">OSCHINA开源中国</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/wiseai/the-way-to-go_ZH_CN/blob/master/eBook/directory.md" target="_blank">the way to go</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://topgoer.com/" target="_blank">golang文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://goframe.org/display/gf" target="_blank">GoFrame</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyundrive.com/" target="_blank">阿里云盘</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cn.vuejs.org/" target="_blank">vue3文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://element-plus.gitee.io/zh-CN/" target="_blank">element-plus文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.runoob.com/vue3/vue3-tutorial.html" target="_blank">vue3菜鸟教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://v5.bootcss.com/" target="_blank">bootstrap v5 中文文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.bootstrap.cn/" target="_blank">bootstrap文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://caddy2.dengxiaolong.com/docs/" target="_blank">caddy中文教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.58pic.com/" target="_blank">千图</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://ifonts.com/" target="_blank">iFonts</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://marketing.qiniu.com/" target="_blank">七牛云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.ixigua.com/" target="_blank">西瓜视频</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/yugang/index.html" target="_blank">鱼缸计算</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://justcc.mengkang.net/#/" target="_blank">C语言JustCC</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/pptist/" target="_blank">PPTist</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="/index.xml" target="_blank">RSS</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.kancloud.cn/idcpj/python/418553" target="_blank">参考资料</a>
        
    </nav>

    <div class="copyright text-center">
      <span class="power-by">
        Powered by <a class="links" href="https://gohugo.io" target="_blank">Hugo</a>
    </span>
    <span>|</span>
    <span>
        Theme - <a class="links" href="https://github.com/wiseai-go/blog-hugo" target="_blank">WiseAI</a>
    </span>
    <br>
    <span class="copyright-year">
        &copy;
        
        2017 -
        2023<span>
            陇ICP备15000157号
            
        </span></span>

</div>
</footer>
<script src="/js/bootstrap.bundle.min.js"></script>


</body>
</html>
